package com.leyou.page.service.impl;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.GoodsPageService;
import com.leyou.page.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String SPU_KEY_PREFIX = "page:spu:id:";
    private static final String SKU_KEY_PREFIX = "page:sku:id:";
    private static final String DETAIL_KEY_PREFIX = "page:detail:id:";
    private static final String CATEGORY_KEY_PREFIX = "page:category:id:";
    private static final String BRAND_KEY_PREFIX = "page:brand:id:";
    private static final String SPEC_KEY_PREFIX = "page:spec:id:";

    @Override
    public String loadSpuData(Long spuId) {
        // 1.远程查询数据
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        // 2.将查询到数据转为VO
        SpuVO spuVO = spuDTO.toEntity(SpuVO.class);
        spuVO.setCategoryIds(spuDTO.getCategoryIds());
        // 3.把vo转为json
        String json = JsonUtils.toJson(spuVO);
        // 4.写入redis
        redisTemplate.opsForValue().set(SPU_KEY_PREFIX+spuId,json);
        // 5.返回
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        // 1.远程查询数据
        SpuDetailDTO spuDetailDTO = itemClient.querySpuDetailById(spuId);
        // 3.把vo转为json
        String json = JsonUtils.toJson(spuDetailDTO);
        // 4.写入redis
        redisTemplate.opsForValue().set(DETAIL_KEY_PREFIX+spuId,json);
        // 5.返回
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        // 1.远程查询数据
        List<SkuDTO> skuDTOList = itemClient.querySkuBySpuId(spuId);
        // 3.转为json
        String json = JsonUtils.toJson(skuDTOList);
        // 4.写入redis
        System.out.println("json = " + json);
        redisTemplate.opsForValue().set(SKU_KEY_PREFIX+spuId,json);
        // 5.返回
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {

        if (ids.size() != 3){
            // 数据格式有误
            throw new LyException(400,"商品分类需要三级id");
        }
        // 1.远程查询数据
        List<CategoryDTO> categoryDTOList = itemClient.queryCategoryByIds(ids);
        // 2.将查询到数据转为VO
        List<CategoryVO> categoryVOList = categoryDTOList.stream()
                .map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class))
                .collect(Collectors.toList());
        // 3.把vo转为json
        String json = JsonUtils.toJson(categoryVOList);
        // 4.写入redis
        redisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX+ids.get(2),json);
        // 5.返回
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        // 1.远程查询数据
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        // 2.将查询到数据转为VO
        BrandVO brandVO = brandDTO.toEntity(BrandVO.class);
        // 3.把vo转为json
        String json = JsonUtils.toJson(brandVO);
        // 4.写入redis
        redisTemplate.opsForValue().set(BRAND_KEY_PREFIX+brandId,json);
        // 5.返回
        return json;
    }

    @Override
    public String loadSpecData(Long categoryId) {
        // 1.远程查询数据
        // 调用方法   根据分类id查询规格组及规格参数
        List<SpecGroupDTO> specGroupDTOList = itemClient.querySpecList(categoryId);
        // 2.将查询到数据转为VO  两层转换
        List<SpecGroupVO> specGroupVOList = new ArrayList<>(specGroupDTOList.size());
        for (SpecGroupDTO specGroupDTO : specGroupDTOList) {
            //2.1  将specGroupDTO转换为SpecGroupVO
            SpecGroupVO specGroupVO = new SpecGroupVO();
            specGroupVO.setName(specGroupDTO.getName());
            //2.2  将specGroupDTO下的List<SpecParamDTO>属性转换为List<SpecParamVO>
            List<SpecParamDTO> specParamDTOList = specGroupDTO.getParams();
            List<SpecParamVO> specParamVOList = specParamDTOList.stream()
                    .map(specParamDTO -> specParamDTO.toEntity(SpecParamVO.class))
                    .collect(Collectors.toList());
            specGroupVO.setParams(specParamVOList);

            specGroupVOList.add(specGroupVO);
        }
        // 3.把vo转为json
        String json = JsonUtils.toJson(specGroupVOList);
        // 4.写入redis
        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX+categoryId,json);
        // 5.返回
        return json;
    }

    @Override
    public void deleteSku(Long spuId) {
        redisTemplate.delete(SKU_KEY_PREFIX+spuId);
    }


}
